#!/bin/sh
# LambdaNative - a cross-platform Scheme framework
# Copyright (c) 2009-2016, University of British Columbia
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the
# following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# * Neither the name of the University of British Columbia nor
# the names of its contributors may be used to endorse or
# promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

rmifexists "$apptgtdir"
mkdir "$apptgtdir"
tmpdir=`mktemp -d $SYS_TMPDIR/tmp.XXXXXX`
if [ `is_gui_app` = yes ]; then
  cp loaders/x11/x11_microgl.c $tmpdir 
fi
if [ `is_standalone_app` = "no" ]; then
  cp loaders/common/main.c $tmpdir
fi
cd $tmpdir

make_soundfile_freebsd()
{
  mkdir -p "$apptgtdir/sounds"
  cp "$1" "$apptgtdir/sounds"
}
make_sounds make_soundfile_freebsd

echo " => compiling application.."
tgt=$apptgtdir/$SYS_APPNAME$SYS_EXEFIX
if [ `is_standalone_app` = "yes" ]; then
  veval "$SYS_CC -I$SYS_PREFIX/include \
    $cflag_additions -DUSECONSOLE -o $tgt \
    -L$SYS_PREFIX/lib $ldflag_additions -lpayload  -lutil -lpthread -lm"
else
  if [ `is_gui_app` = yes ]; then
    veval "$SYS_CC -I$SYS_PREFIX/include \
      $cflag_additions x11_microgl.c main.c -o $tgt \
      -L/usr/local/lib \
      -L/usr/local/lib/X11 \
      -L$SYS_PREFIX/lib $ldflag_additions -lpayload -lGL -lXext -lX11 -lutil -lpthread -lm"
  else
    veval "$SYS_CC -I$SYS_PREFIX/include \
      $cflag_additions -DUSECONSOLE main.c -o $tgt \
      -L$SYS_PREFIX/lib $ldflag_additions -lpayload  -lutil -lpthread -lm"
  fi
fi
asserterror $?
assertfile $tgt
if [ ! "X$SYS_MODE" = "Xdebug" ]; then
  $SYS_STRIP $tgt
  asserterror $?
fi
cd $here

echo " => cleaning up.."
rm -rf "$tmpdir"

#eof
